Skip to content

fix: handle zero-sized memory intrinsics#876

Open
mizoz wants to merge 2 commits intorust-lang:masterfrom
mizoz:fix/zero-sized-memory-builtins
Open

fix: handle zero-sized memory intrinsics#876
mizoz wants to merge 2 commits intorust-lang:masterfrom
mizoz:fix/zero-sized-memory-builtins

Conversation

@mizoz
Copy link
Copy Markdown

@mizoz mizoz commented Apr 28, 2026

Fixes #516.

This avoids lowering zero-sized copy, copy_nonoverlapping, and write_bytes intrinsics to GCC memory builtins. For zero-sized element types, the operation has no bytes to touch, so the backend can return before the fallback lowering emits memcpy, memmove, or memset.

A small run test covers the three zero-sized memory intrinsic paths using () pointers.

Tested with:

  • git diff --check
  • cargo fmt --check
  • ./y.sh test --mini-tests
  • cargo test -p rustc_codegen_gcc --test lang_tests zero_sized_mem -- --nocapture

Comment thread src/intrinsic/mod.rs Outdated
return Ok(());
}
return Err(Instance::new_raw(instance.def_id(), instance.args));
}
Copy link
Copy Markdown
Member

@bjorn3 bjorn3 Apr 28, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not change the memset, memcpy and memmove methods on Builder? Also this only helps with ZST types, not with copying zero elements.

View changes since the review

@mizoz
Copy link
Copy Markdown
Author

mizoz commented Apr 28, 2026

You're right — this should cover the generic memcpy/memmove/memset paths and not only the ZST intrinsic case.

I had avoided changing the builder methods because a first pass that split the current block inside those methods caused later MIR emission to keep writing to the terminated block in local testing. I'm going to rework this instead of trying to paper over it, so the updated patch handles runtime zero sizes cleanly as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Make sure memcpy/memmove/memset with size 0 behave correctly

2 participants